#include <QApplication>
#include <QTableView>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QComboBox>
#include <QSpinBox>
#include <QDebug>
#include <QWidget>
// 自定义性别代理
class GenderDelegate : public QStyledItemDelegate {
public:
    GenderDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {};
    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const override {
        QComboBox* editor = new QComboBox(parent);
        editor->addItems({"男", "女"});
        return editor;
    }

    void setEditorData(QWidget* editor, const QModelIndex& index) const override {
        QComboBox* combo = qobject_cast<QComboBox*>(editor);
        combo->setCurrentText(index.data().toString());
    }

    void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override {
        QComboBox* combo = qobject_cast<QComboBox*>(editor);
        model->setData(index, combo->currentText());
    }
};

// 自定义成绩代理
class ScoreDelegate : public QStyledItemDelegate {
public:
    ScoreDelegate(QObject* parent = nullptr) : QStyledItemDelegate(parent) {};
    QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem&, const QModelIndex&) const override {
        QSpinBox* editor = new QSpinBox(parent);
        editor->setRange(0, 100);
        return editor;
    }

    void setEditorData(QWidget* editor, const QModelIndex& index) const override {
        QSpinBox* spin = qobject_cast<QSpinBox*>(editor);
        spin->setValue(index.data().toInt());
    }

    void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override {
        QSpinBox* spin = qobject_cast<QSpinBox*>(editor);
        model->setData(index, spin->value());
    }
};

// 主窗口类
class MainWindow : public QWidget {
public:
    MainWindow(QWidget* parent = nullptr) : QWidget(parent) {
        QVBoxLayout* layout = new QVBoxLayout(this);
        QTableView* tableView = new QTableView(this);
        QPushButton* exportBtn = new QPushButton("导出数据", this);

        // 创建模型
        QStandardItemModel* model = new QStandardItemModel(0, 4, this);
        model->setHorizontalHeaderLabels({"姓名", "学号", "性别", "成绩"});

        // 添加初始数据
        QList<QStringList> students = {
            {"张三", "1001", "男", "92"},
            {"李四", "1002", "女", "85"},
            {"王五", "1003", "男", "78"},
        };

        for (const QStringList& row : students) {
            QList<QStandardItem*> items;
            for (const QString& cell : row) {
                items << new QStandardItem(cell);
            }
            model->appendRow(items);
        }

        // 设置模型和代理
        tableView->setModel(model);
        tableView->setItemDelegateForColumn(2, new GenderDelegate(this));
        tableView->setItemDelegateForColumn(3, new ScoreDelegate(this));
        tableView->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);

        // 导出按钮槽函数
        connect(exportBtn, &QPushButton::clicked, this, [=]() {
            qDebug() << "导出学生数据：";
            for (int row = 0; row < model->rowCount(); ++row) {
                QStringList data;
                for (int col = 0; col < model->columnCount(); ++col) {
                    data << model->item(row, col)->text();
                }
                qDebug().noquote() << data.join(" | ");
            }
        });

        // 布局
        layout->addWidget(tableView);
        layout->addWidget(exportBtn);
        setLayout(layout);
        setWindowTitle("学生成绩管理");
        resize(500, 300);
    }
};

int main(int argc, char* argv[]) {
    QApplication app(argc, argv);
    MainWindow w;
    w.show();
    return app.exec();
}
